package com.mcptt.config.service.sub;

import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.service.CallPriority;
import com.mcptt.config.service.sub.sub.EmergencyCall;
import com.mcptt.config.service.sub.sub.EmergencyResourcePriority;
import com.mcptt.config.service.sub.sub.FcTimersCounters;
import com.mcptt.config.service.sub.sub.FloorControlQueue;
import com.mcptt.config.service.sub.sub.ImminentPerilResourcePriority;
import com.mcptt.config.service.sub.sub.NormalResourcePriority;
import com.mcptt.config.service.sub.sub.PrivateCall;
import com.mcptt.config.service.sub.sub.ProtectionBetweenMcpttServers;
import com.mcptt.config.service.sub.sub.SignallingProtection;
import com.mcptt.config.service.sub.sub.TransmitTime;

public class OnNetwork {
	
	public static final String Name = "on-network";
	private static final String Name_NumLevelsPriorityHierarchy = "num-levels-priority-hierarchy";
	private static final String Name_HangTimeWarning = "hang-time-warning";
	
	private EmergencyCall emergencyCall = null;
	private PrivateCall privateCall = null;
	private int numLevelsPriorityHierarchy = 0;
	private TransmitTime transmitTime = null;
	private String hangTimeWarning = null;
	private FloorControlQueue floorControlQueue = null;
	private FcTimersCounters fcTimersCounters = null;
	private SignallingProtection signallingProtection = null;
	private ProtectionBetweenMcpttServers protectionBetweenMcpttServers = null;
	private EmergencyResourcePriority emergencyResourcePriority = null;
	private ImminentPerilResourcePriority imminentPerilResourcePriority = null;
	private NormalResourcePriority normalResourcePriority = null;


	
	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);
			
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			
			if(name.equals(EmergencyCall.Name))
			{
				emergencyCall = new EmergencyCall();
				emergencyCall.parse(subElem);
			}
			else if(name.equals(PrivateCall.Name))
			{
				privateCall = new PrivateCall();
				privateCall.parse(subElem);
			}
			else if(name.equals(Name_NumLevelsPriorityHierarchy))
			{
				String value = subElem.getText();
				numLevelsPriorityHierarchy = CheckData.parseInt(value);
			}
			else if(name.equals(TransmitTime.Name))
			{
				transmitTime = new TransmitTime();
				transmitTime.parse(subElem);
			}
			else if(name.equals(Name_HangTimeWarning))
			{
				hangTimeWarning = subElem.getText();
			}
			else if(name.equals(FloorControlQueue.Name))
			{
				floorControlQueue = new FloorControlQueue();
				floorControlQueue.parse(subElem);
			}
			else if(name.equals(FcTimersCounters.Name))
			{
				fcTimersCounters = new FcTimersCounters();
				fcTimersCounters.parse(subElem);
			}
			else if(name.equals(SignallingProtection.Name))
			{
				signallingProtection = new SignallingProtection();
				signallingProtection.parse(subElem);
			}
			else if(name.equals(ProtectionBetweenMcpttServers.Name))
			{
				protectionBetweenMcpttServers = new ProtectionBetweenMcpttServers();
				protectionBetweenMcpttServers.parse(subElem);
			}
			else if(name.equals(EmergencyResourcePriority.Name))
			{
				emergencyResourcePriority = new EmergencyResourcePriority();
				emergencyResourcePriority.parse(subElem);
			}
			else if(name.equals(ImminentPerilResourcePriority.Name))
			{
				imminentPerilResourcePriority = new ImminentPerilResourcePriority();
				imminentPerilResourcePriority.parse(subElem);
			}
			else if(name.equals(NormalResourcePriority.Name))
			{
				normalResourcePriority = new NormalResourcePriority();
				normalResourcePriority.parse(subElem);
			}
		}  
	}
	
	public CallPriority toCallPriority()
	{
		int emergencyPriority = -1;
		int imminentPriority = -1;
		int normalPriority = -1;
		if(emergencyResourcePriority != null)
		{
			emergencyPriority =  emergencyResourcePriority.getResourcePriorityPriority();
		}
		if(imminentPerilResourcePriority != null)
		{
			imminentPriority = imminentPerilResourcePriority.getResourcePriorityPriority();
		}
		if(normalResourcePriority != null)
		{
			normalPriority = normalResourcePriority.getResourcePriorityPriority();
		}
		CallPriority callPriority = new CallPriority(emergencyPriority, imminentPriority, normalPriority);
		Log.debug(this.getClass(), "toCallPriority  callPriority= " + callPriority);
		return callPriority;
	}
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name).append(">").append("\r\n");
		
		strBuf.append(emergencyCall);
		strBuf.append(privateCall);
		
		strBuf.append("    ").append("<").append(Name_NumLevelsPriorityHierarchy).append(">");
		strBuf.append(numLevelsPriorityHierarchy);
		strBuf.append("</").append(Name_NumLevelsPriorityHierarchy).append(">").append("\r\n");
		
		strBuf.append(transmitTime);
		
		strBuf.append("    ").append("<").append(Name_HangTimeWarning).append(">");
		strBuf.append(hangTimeWarning);
		strBuf.append("</").append(Name_HangTimeWarning).append(">").append("\r\n");
		
		strBuf.append(floorControlQueue);
		strBuf.append(fcTimersCounters);
		strBuf.append(signallingProtection);
		strBuf.append(protectionBetweenMcpttServers);
		strBuf.append(emergencyResourcePriority);
		strBuf.append(imminentPerilResourcePriority);
		strBuf.append(normalResourcePriority);
		strBuf.append("\r\n").append("<anyExt/>");
		
		strBuf.append("</").append(Name).append(">").append("\r\n");

		return strBuf.toString();
	}
}
